package nuc.ss.wl.controller;

import nuc.ss.wl.entity.Class;
import nuc.ss.wl.service.*;
import nuc.ss.wl.service.impl.ClassServiceImpl;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

public class ClassServlet extends BaseServlet {

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int classno = Integer.parseInt(request.getParameter("classno"));
        int tno = Integer.parseInt(request.getParameter("tno"));
        String classname = request.getParameter("classname");
        int classgrade = Integer.parseInt(request.getParameter("classgrade"));
        Class clazz = new Class();
        clazz.setClassno(classno);
        clazz.setTno(tno);
        clazz.setClassname(classname);
        clazz.setClassgrade(classgrade);
        ClassService classService = new ClassServiceImpl();
        int i = classService.add(clazz);
        if (i > 0) {
            response.sendRedirect(request.getContextPath()+ "/classServlet?action=findAll");
        } else {
            /*request.setAttribute("error","添加课程失败");
            request.getRequestDispatcher("").forward(request, response);*/
        }

    }

    public void findAll(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException {
        ClassService classService = new ClassServiceImpl();
        List<Class> list = classService.findAll();
        request.setAttribute("classList", list);
        request.getRequestDispatcher("/pages/manager/class_manager.jsp").forward(request, response);
    }

    public void delete(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        int classno = Integer.parseInt(request.getParameter("classno"));
        ClassService classService = new ClassServiceImpl();
        classService.delete(classno);
        request.getRequestDispatcher("/servlet/DepartmentServlet?method=findAll").forward(request, response);
    }
    /**
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findClass(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        int classno = Integer.parseInt(request.getParameter("classno"));
        ClassService classService = new ClassServiceImpl();
        Class clazz = classService.findById(classno);
        request.setAttribute("clazz", clazz);
        request.getRequestDispatcher("/pages/manager/class_choose_course.jsp").forward(request, response);
    }
    /**
     * 更新数据
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void update(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //接收查询条件
        int classno = Integer.parseInt(request.getParameter("classno"));
        int tno = Integer.parseInt(request.getParameter("tno"));
        String classname = request.getParameter("classname");
        int classgrade = Integer.parseInt(request.getParameter("classgrade"));
        //调用业务层(完成添加)进行处理
        ClassService classService = new ClassServiceImpl();
        Class clazz = new Class();
        clazz.setClassno(classno);
        clazz.setTno(tno);
        clazz.setClassname(classname);
        clazz.setClassgrade(classgrade);
        int i = classService.update(clazz);

        //根据结果进行页面跳转
        if(i>0){
            response.sendRedirect(request.getContextPath()+"/servlet/EmployeeServlet?method=findEmp");
        }else{
            request.setAttribute("error","更新班级失败");
            request.getRequestDispatcher("/system/empUpdate.jsp").forward(request, response);
        }
    }


    public void exportXLS(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取三个查询条件

        //调用业务层完成查询操作
        ClassService classService = new ClassServiceImpl();
        List<Class> classList = classService.findAll();
        //返回json字符串
        createExcel(classList,response);
    }
    /**
     * 创建Excel
     * @param classList
     *            数据
     */
    private static void createExcel( List<Class> classList,HttpServletResponse response) {
        // 创建一个Excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建一个工作表
        HSSFSheet sheet = workbook.createSheet("班级信息表1");
        CellRangeAddress region = new CellRangeAddress(0, // first row
                0, // last row
                0, // first column
                3 // last column
        );
        sheet.addMergedRegion(region);

        HSSFRow hssfRow = sheet.createRow(0);
        HSSFCell headCell = hssfRow.createCell(0);
        headCell.setCellValue("班级详细信息");

        // 设置单元格格式居中
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        headCell.setCellStyle(cellStyle);


        // 添加表头行
        hssfRow = sheet.createRow(1);
        // 添加表头内容
        headCell = hssfRow.createCell(0);
        headCell.setCellValue("班级号");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(1);
        headCell.setCellValue("班主任");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(2);
        headCell.setCellValue("班级名");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(3);
        headCell.setCellValue("年级");
        headCell.setCellStyle(cellStyle);

        // 添加数据内容
        for (int i = 0; i < classList.size(); i++) {
            hssfRow = sheet.createRow((int) i + 2);
            Class clazz = classList.get(i);

            // 创建单元格，并设置值
            HSSFCell cell = hssfRow.createCell(0);
            cell.setCellValue(clazz.getClassno());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(1);
            cell.setCellValue(clazz.getTno());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(2);
            cell.setCellValue(clazz.getClassname());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(3);
            cell.setCellValue(clazz.getClassgrade());
            cell.setCellStyle(cellStyle);

        }

        // 保存Excel文件
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment; fileName=class.xls" );//附件形式下载，文件名叫duty.xls
            //OutputStream outputStream = new FileOutputStream("D:/class1.xls");  保存到了服务器端
            OutputStream outputStream = response.getOutputStream();//保存到客户端
            workbook.write(outputStream);
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
